package pers.dzk.fivelnarow.judge;

import pers.dzk.fivelnarow.piece.PieceType;

import java.io.Serializable;

import static pers.dzk.fivelnarow.window.MainWindow.*;
/**
 * @author 丁泽凯
 */
public class Judge extends JudgeSuper implements Serializable {
    private int max = 5;
    public Judge(){}
    @Override
    public void execute() {
        if(isValidTime()){
            if(getPlayer1().getCount() + getPlayer2().getCount() == 0){
                setOver(true);
            }
            PieceType type;
            if(getTimeCount() >= max+max-1){
                if((type = searchCheckerboard()) != PieceType.空){
                    setOver(true);
                    if(type == getPlayer1().getPieceType()){
                        setVictoryPlayer(getPlayer1());
                        setFailPlayer(getPlayer2());
                    }else{
                        setVictoryPlayer(getPlayer2());
                        setFailPlayer(getPlayer1());
                    }
                }
            }
        }
    }
    private PieceType searchCheckerboard(){
        int l = getCheckerboard().getPieceMatrix().length;
        int s = l/2;
        int x = s,y = s,c = s;
        PieceType type;
        if((type = searchPiece(y,x)) != PieceType.空){
            return type;
        }
        for (int i = 1; i <= s; i++){
            int p = i*2;
            x--;
            for (int j = 0; j < p; j++){
                if((type = searchPiece(y,x)) != PieceType.空){
                    return type;
                }
                if((type = searchPiece(x,y)) != PieceType.空){
                    return type;
                }
                y++;
            }
            y--;
            x++;
            for (int j = 0; j < p-1; j++){
                if((type = searchPiece(y,x)) != PieceType.空){
                    return type;
                }
                if((type = searchPiece(x,y)) != PieceType.空){
                    return type;
                }
                x++;
            }
            c++;
            if((type = searchPiece(c,c)) != PieceType.空){
                return type;
            }
            if((type = searchPiece(c-p,c-p)) != PieceType.空){
                return type;
            }
            y = c-p;
            x = y;
        }
        return PieceType.空;
    }
    private PieceType searchPiece(int a, int b){
        int max = this.max-1;
        int l = getCheckerboard().getPieceMatrix().length;
        PieceType type = getCheckerboard().getPieceMatrix()[a][b].getCode();
        if(type == PieceType.空) {
            return type;
        }
        boolean isfive = true;
        if(b + max < l){
            for (int i = 1; i <= max;i++){
                if(getCheckerboard().getPieceMatrix()[a][b+i].getCode() != type){
                    isfive = false;
                    break;
                }
            }
            if(isfive){
                return type;
            }
            isfive = true;
        }
        if(a - max > -1 && b - max > -1){
            for (int i = 1; i <= max;i++) {
                if (getCheckerboard().getPieceMatrix()[a-i][b-i].getCode() != type) {
                    isfive = false;
                    break;
                }
            }
            if(isfive){
                return type;
            }
            isfive = true;
        }
        if(a - max > -1){
            for (int i = 1; i <= max;i++) {
                if (getCheckerboard().getPieceMatrix()[a-i][b].getCode() != type) {
                    isfive = false;
                    break;
                }
            }
            if(isfive){
                return type;
            }
            isfive = true;
        }
        if(a - max > -1 && b + max < l){
            for (int i = 1; i <= max;i++) {
                if (getCheckerboard().getPieceMatrix()[a-i][b+i].getCode() != type) {
                    isfive = false;
                    break;
                }
            }
            if(isfive){
                return type;
            }
        }
        return PieceType.空;
    }

}
